using System;
using Madason.MetaModel;
using Madason.MetaModel.Validation;
using System.Collections;
using System.IO;
using System.Collections.Generic;


namespace Madason.MetaModel.Maven
{

	/// <summary>
	/// Summary description for MavenProject
	/// </summary>
	public class MavenProject : Namespace
	{
		

		public string GroupId
		{
			get 
			{
				string val = FindValue("groupId");
				if (val == null || val == "") val = Parent.FullName;
				return val;
			}
		}

		public string ArtifactId
		{
			get
			{
				string val = FindValue("artifactId");
				if (val == null || val == "") val = Name;
				return val;
			}
		}

		public string Version
		{
			get
			{
				string val = FindValue("version");
				if ((val == null || val == "") && Parent is MavenProject)
					return (Parent as MavenProject).Version;
				return val;
			}
		}

		public bool AutoAddDepdendencies
		{
			get
			{
				string val = FindValue("autoAddDependencies");
				if (val == null || val == "") return true;
				if (val.ToLower() == "false") return false;
				return true;
			}
		}
		
		public virtual string Plugins
		{
			get
			{
				return FindValue("plugins");
			}
		}


		public MavenProject ParentProject
		{
			get
			{
				if (Parent is MavenProject)
					return Parent as MavenProject;
				else
					return null;
			}
		}

		public virtual string Packaging
		{
			get
			{
				if (StereotypeName == "MavenMaster")
					return "pom";
				else if (StereotypeName == "MavenJAR")
					return "jar";
				else if (StereotypeName == "MavenEJB")
					return "ejb";
				else if (StereotypeName == "MavenWAR")
					return "war";
				else throw new ArgumentException("unknown stereotype " + StereotypeName);
			}
		}

		public MavenProjectList SubProjects
		{
			get
			{
				MavenProjectList list = new MavenProjectList();
				foreach (Namespace ns in Namespaces)
				{
					if (ns is MavenProject)
						list.Add(ns);
				}
				return list;
			}
		}

		public virtual List<MavenDependency> MavenDependencies
		{
			get
			{
				List<MavenDependency> list = new List<MavenDependency>();
				foreach (Dependency dep in this.Dependencies)
				{					
					if (dep.Target is MavenProject)
					{
						string scope = "";
						if (dep.HasValue("Scope"))
							scope = dep.FindValue("Scope");
						MavenProject mavenProject = dep.Target as MavenProject;
						list.Add(new MavenDependency(mavenProject.GroupId, mavenProject.ArtifactId, mavenProject.Version, scope));
					}
				}
				return list;
			}
		}


		public virtual void GenerateDirectoryStructure(DirectoryInfo dir)
		{
		}


		public override void Validate(Madason.MetaModel.Validation.ValidationIssueList issues)
		{
			if ((Version == null || Version == "") && !(ParentProject is MavenProject))
			{
				issues.Add(new ValidationIssue("Project has no Version", ValidationIssueSeverity.Error, this));
			}
			base.Validate(issues);
		}
	}

	public class MavenProjectList : ArrayList
	{
		public new MavenProject this[int i]
		{
			get { return base[i] as MavenProject; }
		}
	}

}